<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Class: C_menus
 * 
 * untuk mempersiapkan kontrol menu yang akan ditampilkan
 * 
 * @modul 	Gen Menu
 * @author 	Eko Junaidi Salam
 * @contact +62 856 4843 0689
 */
class C_menus extends CI_Controller {

	function __construct(){
		parent::__construct();
	}
	
	function index()
	{
		if($this->session->userdata('group_id') == 0){
			$sql = "SELECT * FROM vu_tree_menus";
		}else{
			$sql = "SELECT * FROM vu_tree_menus
				JOIN s_permissions ON(s_permissions.PERM_MENU = vu_tree_menus.MENU_ID
					AND s_permissions.PERM_GROUP = ".$group_id."
					AND s_permissions.PERM_PRIV IS NOT NULL)";
		}
		
		$result = $this->db->query($sql);
		$rows = $result->result();
		$kunci = array();
		
		$root = array('expanded' => 'true','children' => array());
		$menu = new stdClass();
		$menu->id = NULL;
		$menu->text = NULL;
		$menu->iconCls = NULL;
		$menu->expanded = NULL;
		$menu->children = array();
		
		foreach($rows as $v){
			if($v->menu_parent == '0'){
				$menu->id = $v->menu_kode;
				$menu->text = $v->menu_title;
				$menu->iconCls = NULL;
				$menu->expanded = true;
				$menu->children = array();
				
				array_push($root['children'],$menu);
				array_push($kunci,$v->menu_position);
				$menu = new stdClass();
			}
		}
		//echo $root['children'][0]->text."<br />";
		$child = new stdClass();
		
		for($i=0;$i<sizeof($root['children']);$i++)
		{
			foreach($rows as $v){
				if($v->menu_parent == $kunci[$i]){
					$child->id = $v->menu_kode;
					$child->text = $v->menu_title;
					$child->iconCls = NULL;
					$child->leaf = true;
					
					array_push($root['children'][$i]->children,$child);
					$child = new stdClass();
				}
			}
		}
		//$result = array('success' => true,'Root' => $root);
		
		/*if ( ! write_file("./assets/menu.json", json_encode($root)))
		{
			return 0;
		}
		else
		{
			echo "<strong>Menu telah digenerate...!!!<br /> Lokasi : ./assets/menu.json </strong><br />";
		}*/
		
		echo json_encode($root);
	}
	
	function getMenus(){
		if($this->session->userdata('group_id') == 0){
			$sql = "SELECT * FROM s_menus";
		}else{
			$sql = "SELECT * FROM vu_tree_menus
				JOIN s_permissions ON(s_permissions.PERM_MENU = vu_tree_menus.MENU_ID
					AND s_permissions.PERM_GROUP = ".$this->session->userdata('group_id')."
					AND s_permissions.PERM_PRIV IS NOT NULL)";
		}
		
		$result = $this->db->query($sql);
		$rows = $result->result();
		$kunci = array();
		
		$root = array('expanded' => 'true','children' => array());
		$menu = new stdClass();
		$menu->id = NULL;
		$menu->text = NULL;
		$menu->iconCls = NULL;
		$menu->expanded = NULL;
		$menu->children = array();
		
		foreach($rows as $v){
			if($v->menu_parent == '0'){
				$menu->id = $v->menu_kode;
				$menu->text = $v->menu_title;
				$menu->iconCls = NULL;
				$menu->expanded = false;
				$menu->children = array();
				
				array_push($root['children'],$menu);
				array_push($kunci,$v->menu_position);
				$menu = new stdClass();
			}
		}
		//echo $root['children'][0]->text."<br />";
		$child = new stdClass();
		
		for($i=0;$i<sizeof($root['children']);$i++)
		{
			foreach($rows as $v){
				if($v->menu_parent == $kunci[$i]){
					$child->id = $v->menu_kode;
					$child->text = $v->menu_title;
					$child->iconCls = NULL;
					$child->leaf = true;
					
					array_push($root['children'][$i]->children,$child);
					$child = new stdClass();
				}
			}
		}
		//$result = array('success' => true,'Root' => $root);
		echo json_encode($root);
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */